package com.lhz.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lhz.services.StaffService;

@Controller
@RequestMapping("/system")
public class SystemControll {
	@Autowired
	StaffService staffService;
	
	/**
	 * 跳到登录界面
	 * @return
	 */
	@RequestMapping("/toLogin")
	public String toLogin() {
		return "login";
	}
	
	/**
	 * 员工登录
	 * @param staffId
	 * @param password
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/login")
	@ResponseBody
	public Map<String,String> login(String staffId,String password,HttpServletRequest request) {
		Map<String,String> map=new HashMap<String, String>();
		String result=staffService.login(staffId, password);
		if(result.equals("用户名或密码错误")) {
			map.put("result", "登录失败");
			map.put("msg", result);
		}else {
			map.put("result", "登录成功");
		}
		return map;
	}
	
	/**
	 * 跳到主页面
	 * @return
	 */
	@RequestMapping("/toMain")
	public String toMain() {
		return "main";
	}
	
	/**
	 * 跳到修改密码页面
	 * @return
	 */
	@RequestMapping("/toUpdatePassword")
	public String toUpdatePassword() {
		return "UpdatePassword";
	}
	
	/**
	 * 修改密码
	 * @param StaffId
	 * @param oldPassword
	 * @param newPassword
	 * @return
	 */
	@RequestMapping("/updatePassword")
	@ResponseBody
	public Map<String,String> updatePassword(String oldPassword,String newPassword){
		Map<String,String> map=new HashMap<>();
		//通过shiro获取用户的id
		String staffId="sta001";
		String result=staffService.updatePassword(staffId, oldPassword, newPassword);
		if(result.equals("修改成功")) {
			map.put("result", "修改成功");
		}else {
			map.put("result", "修改失败");
			map.put("msg", result);
		}
		return map;
	}
	
	/**
	 * 退出登录
	 * @return
	 */
	@RequestMapping("/logout")
	@ResponseBody
	public Map<String,String> logout(HttpSession session) {
		session.removeAttribute("info");
		Map<String,String> map=new HashMap<>();
		map.put("result", "成功退出登录");
		return map;
	}
	
	/**
	 * 切换用户
	 * @return
	 */
	@RequestMapping("/changeUser")
	public String changeUser(HttpSession session) {
		session.removeAttribute("info");
		return "login";
	}
	
}
